package com.frxuexi.report.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.report.R
import com.frxuexi.report.ReportIntent
import com.frxuexi.report.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun ReportCommitPage(navHostController: NavHostController,json:String,vm:ReportViewModel= hiltViewModel()) {
    val entity = Gson().fromJson(json, ReportDialogEntity::class.java)
    Log.i("===",entity.toString())

    var now by remember {
        mutableStateOf("")
    }
    var next by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("提交成功")
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })

    Column(
        Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.lightgray))
            .padding(10.dp)) {
        BaseTitle(title = "日志编辑", navHostController = navHostController)
        val time = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
        val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
        Spacer(modifier = Modifier.height(10.dp))
        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White).padding(start = 10.dp), verticalArrangement = Arrangement.Center ){
            Text(text = "$time $week (今日)",
                textAlign = TextAlign.Center)
        }

        Spacer(modifier = Modifier.height(10.dp))
        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .border(1.dp, Color.LightGray)
                .padding(start = 10.dp), verticalArrangement = Arrangement.Center ) {
            Text(
                text = "今日内容",
            )
        }
        TextField(value = now, onValueChange = {now=it},Modifier.fillMaxWidth().height(100.dp), maxLines = 5,
            label={ Text(text = "写出今日工作内容")},
            colors =  TextFieldDefaults.colors(unfocusedContainerColor= Color.White,
                focusedIndicatorColor= Color.White,unfocusedIndicatorColor=Color.White))
        Spacer(modifier = Modifier.height(10.dp))
        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .border(1.dp, Color.LightGray)
                .padding(start = 10.dp), verticalArrangement = Arrangement.Center ) {
            Text(
                text = "明日计划"
            )
        }
        TextField(value = next, onValueChange = {next=it},Modifier.fillMaxWidth().height(100.dp), maxLines = 5,
            label={ Text(text = "写出明日工作计划")},
            colors =  TextFieldDefaults.colors(unfocusedContainerColor= Color.White,
                focusedIndicatorColor= Color.White,unfocusedIndicatorColor=Color.White))
        Spacer(modifier = Modifier.height(10.dp))
        Button(onClick = {
            val map = mutableMapOf<String, Any>()
            map["reportType"]=1
            map["reportTime"]=System.currentTimeMillis()
            map["reportNowValue"]=now
            map["reportNextValue"]=next
            map["reportNowTitle"]="今日内容"
            map["reportNextTitle"]="明日计划"
            map["reportRemind"]="王女士"
            vm.sendIntent(ReportIntent.addReport(map))
        },Modifier.fillMaxWidth()) {
            Text(text = "提交")
        }
    }
}